package com.yztc.youbadbad.ui.oversea;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.yztc.youbadbad.App;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.OverseaRecyclerAdapter;
import com.yztc.youbadbad.base.BaseFragment;
import com.yztc.youbadbad.bean.oversea.OverseaDataBean;
import com.yztc.youbadbad.bean.oversea.OverseaImgBean;
import com.yztc.youbadbad.bean.oversea.OverseaImgLikeBean;
import com.yztc.youbadbad.presenter.IOverseaPresenter;
import com.yztc.youbadbad.presenter.OverseaPresenterImpl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * kio
 * A simple {@link Fragment} subclass.
 */
public class OverseaAllFragment extends BaseFragment implements IOverseaView {

    @BindView(R.id.oversea_rv)
    RecyclerView overseaRv;

    private Context mContext= App.app;
    private List<Object> mOverseaList=new ArrayList<>();
    //private List<Object> mOversealikeList=new ArrayList<>();
    private OverseaRecyclerAdapter mOverseaAdapter;
    private int mPostion = 0;
    private IOverseaPresenter mIoverseaPresenter;
    private String TAG="====================";

    public static OverseaAllFragment newInstance(int position) {

        Bundle args = new Bundle();
        args.putInt("position", position);
        OverseaAllFragment fragment = new OverseaAllFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPostion = getArguments().getInt("position");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_oversea_all, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mOverseaList.clear();
        initData();
        initView();
    }

    private void initView() {
        final int columnCount = 2;
        mOverseaAdapter=new OverseaRecyclerAdapter(mContext,mOverseaList);

        GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,columnCount);
        overseaRv.setLayoutManager(gridLayoutManager);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mOverseaAdapter.getItemViewType(position) == OverseaRecyclerAdapter.STATE1) {
                    return columnCount;
                }
                return 1;
            }
        });
        overseaRv.setAdapter(mOverseaAdapter);


    }

    private void initData() {
        mIoverseaPresenter=new OverseaPresenterImpl(this);
        mIoverseaPresenter.getOverseaObservable(mPostion);

    }

    @Override
    public void getOverseaObservable(String oversea) {
        mIoverseaPresenter.getOverseaLike(mPostion);
//        数据解析
        try {
            JSONObject overseaObject=new JSONObject(oversea);
            JSONObject data=overseaObject.getJSONObject("data");
            JSONObject ad_list=data.getJSONObject("ad_list");
            JSONObject data2=ad_list.getJSONObject("data");
            JSONArray list=data2.getJSONArray("list");
            for (int i = 0; i < list.length(); i++) {
                getImgBeans(i, list);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mOverseaAdapter.notifyDataSetChanged();
    }

    @Override
    public void getOverseaLike(String overseaLike) {
        Log.i(TAG,",,,,,,,,,,,,"+mOverseaList.size());
        Log.i(TAG,",,,,,,,,,,,,"+overseaLike);
        try {
            JSONObject oversealike=new JSONObject(overseaLike);
            JSONObject data=oversealike.getJSONObject("data");
            String s = data.toString();
            Gson gson=new Gson();
            OverseaDataBean overseaDataBean = gson.fromJson(s, OverseaDataBean.class);
            mOverseaList.addAll(overseaDataBean.getList());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i(TAG,",,,,,,,,,,,,"+mOverseaList.size());
        mOverseaAdapter.notifyDataSetChanged();
    }


    private void getImgBeans(int i, JSONArray data)throws JSONException {
        JSONObject bean = data.getJSONObject(i);
        String str = bean.toString();
        Gson gson = new Gson();
        switch (i) {
            case 0://轮播图要一张
                OverseaImgBean overseaImgBean=gson.fromJson(str,OverseaImgBean.class);
                mOverseaList.add(overseaImgBean);
                break;
            case 1:
                break;
        }
    }

    @Override
    public void showProgressBar() {

    }

    @Override
    public void hideProgressBar() {

    }

    @Override
    public void showErrorMessage() {

    }
}
